import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'

App.mpType = 'app'
const app = new Vue({
	store,
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import uView from './uni_modules/vk-uview-ui';
import store from './store';
import request from "@/utils/request";

export function createApp() {
  const app = createSSRApp(App)

  app.use(uView)
  app.use(store)

  app.provide('$store', store)

  return {
    app
  }
}
// #endif


setInterval(()=>{
  if(store.state.socket == null && uni.getStorageSync('token')){
  	store.dispatch('initSocket')
  }
}, 1000 * 5)

let mainSocket = store.state.state || null
let msgUserIdList = []
uni.onSocketMessage(function (res) {
  console.log('mainjs--msg：' + res.data);
  let temp = res.data;
  if(temp.includes('聊天室人数为')){
    return;
  }
  temp = temp.substring(temp.indexOf('：') + 1) // 截取消息内容
  // 截取：之前的内容
  let sendUserId = res.data.substring(0, res.data.indexOf('：'))
  console.log('main----temp', temp)
  console.log('main----sendUserId', sendUserId)
  getMsg(sendUserId)
});

function getMsg(sendUserId) {
  let myId = uni.getStorageSync('uid')
  request.post('messageRecord/getMessage', {
    sendUserId: Number(sendUserId),
    receiveUserId: myId,
  }).then(res => {
    console.log('main---接收消息',res)
    msgUserIdList = uni.getStorageSync('msgUserIdList') || []
    if(msgUserIdList.includes(sendUserId)){
        return
    }else {
        msgUserIdList.push(Number(sendUserId))
    }
    uni.setStorageSync('msgUserIdList', msgUserIdList)
    uni.setTabBarBadge({
      index: 2
    })
  })
}
